var srh = {
	$list : "",
	$selItem : "",
	$selCondition : "",
	$txtValue : "",
	$txtDate : "",
	$btwnValues : "",
	$btwnDates : "",
	$txtDept : "",
	$focusItem : "",
	itemType : "value",
	conditionType : "custom",
	create : function(items, conditions) {
		var html = [];
		$.each(items, function(k, v) {
					html.push("<option value='" + k + "'>" + v + "</option>");
				});
		this.$selItem.html(html.join(""));

		html = [];
		$.each(conditions, function(k, v) {
					html.push("<option value='" + k + "'>" + v + "</option>");
				});

		this.$selCondition.html(html.join(""));
	},
	isNull : function() {
		var t = this, check = {
			'custom-value' : function() {

				if (t.$txtValue.val() == "") {
					swss.ui.tip("请输入比较值");
					return true;
				} else {
					return false;
				}
			},
			'custom-date' : function() {
				if (t.$txtDate.val() == "") {
					swss.ui.tip("请输入比较值");
					return true;
				} else {
					return false;
				}
			},
			'between-value' : function() {

				if ((t.$btwnValues.eq(0).val() == "")
						&& (t.$btwnValues.eq(1).val() == "")) {
					swss.ui.tip("请输入比较值");
					return true;

				} else {

					return false;
				}
			},
			'between-date' : function() {

				if ((t.$btwnDates.eq(0).val() == "")
						&& (t.$btwnDates.eq(1).val() == "")) {
					swss.ui.tip("请输入比较值");
					return true;

				} else {

					return false;
				}
			},
			dept : function() {
				return false;

			}
		};

		return check[t.conditionType + "-" + t.itemType]();

	},
	getItemValueHtml : function() {

		return {
			'custom-value' : '<span class="td4" data="' + this.$txtValue.val()
					+ '">' + this.$txtValue.val() + '</span>',
			'custom-date' : '<span class="td4" data="' + this.$txtDate.val()
					+ '">' + this.$txtDate.val() + '</span>',
			'between-value' : '<span class="td4" data="'
					+ this.$btwnValues.eq(0).val() + ','
					+ this.$btwnValues.eq(1).val() + '">在'
					+ this.$btwnValues.eq(0).val() + '与'
					+ this.$btwnValues.eq(1).val() + '之间</span>',
			'between-date' : '<span class="td4" data="'
					+ this.$btwnDates.eq(0).val() + ','
					+ this.$btwnDates.eq(1).val() + '">在'
					+ this.$btwnDates.eq(0).val() + '与'
					+ this.$btwnDates.eq(1).val() + '之间</span>',
			dept : '<span class="td4" data="' + $("input[name='dept']").val()
					+ '">' + this.$txtDept.val() + '</span>'
		}[this.conditionType + "-" + this.itemType];
	},
	getItemHtml : function(logical, text) {

		return '<div class="tr" ><span class="td1" data="" >左</span><span class="td2" data=" '
				+ logical
				+ ' " >'
				+ text
				+ '</span><span class="td3" data="'
				+ this.$selItem.val()
				+ '" >'
				+ this.$selItem.find("option:selected").text()
				+ '</span><span class="td2" data="'
				+ this.$selCondition.val()
				+ '" >'
				+ this.$selCondition.find("option:selected").text()
				+ '</span>'
				+ this.getItemValueHtml()
				+ '<span class="td1" data="">右</span></div>';
	},

	getHql : function() {
		var hql = "", temp = "", isBetween = false, item = "", btwVal = [];
		this.$list.find("div.tr").each(function(i, item) {

			isBetween = false;

			$(item).find("span").each(function(j, c) {
				temp = $(c).attr("data");

				if (temp != "") {
					if (j == 2) {
						item = temp;
					}
					if (j == 3 && temp == "between") {
						isBetween = true;
					}

					if (j == 4) {
						if (isBetween) {
							btwVal = $(c).attr("data").split(",");
							hql += ">='" + btwVal[0] + "' AND " + item + "<='"
									+ btwVal[1] + "'";
						} else {
							hql += "'" + $(c).attr("data") + "'";
						}
					} else {
						if (!isBetween) {
							hql += $(c).attr("data");
						}
					}

				}
			});

		});
		return hql;
	},
	isFirst : function() {

		if (this.$list.find("div.tr").size() == 0) {
			return true;
		}
		return false;
	},
	add : function() {
		if (!this.isNull()) {
			this.$list.append(this.getItemHtml("", ""));
			$("div.cus").hide();
			$("div.cdt").show();
		}

	},
	addAnd : function() {

		if (!this.isNull()) {
			if (this.isFirst()) {
				this.$list.append(this.getItemHtml("", ""));
			} else {
				this.$list.append(this.getItemHtml("AND", "并且"));
			}
		}
	},
	addOr : function() {
		if (!this.isNull()) {
			if (this.isFirst()) {
				this.$list.append(this.getItemHtml("", ""));
			} else {
				this.$list.append(this.getItemHtml("OR", "或者"));
			}
		}
	},
	resetFirst:function(){
		var $f=this.$list.find("div.tr").eq(0);
		$f.find('span').eq(1).attr("data"," ").html("");
	},
	del : function() {
		if (!this.isFirst()) {
			if (this.$focusItem && this.$focusItem != "") {
				this.$focusItem.remove();
				this.resetFirst();
				if (this.$list.find("div.tr").size() == 0) {
					$("div.cus").show();
					$("div.cdt").hide();
				}
			} else {
				swss.ui.tip("请选择要删除的条件");
			}
		}
	},
	setItemType : function(v) {
		var t = this;
		if (/buyDate/.test(v)) {
			t.itemType = "date";
		} else if (/deptNo/.test(v)) {
			t.itemType = "dept";
		} else {
			t.itemType = "value";
		}

	},
	selCtrl : function() {

		var t = this;

		// $("#treeBox").hide();
		// t.$selCondition.removeAttr("disabled");
		var $inputs = $("#inputs").children();

		$inputs.hide();

		$("#" + t.conditionType + "-" + t.itemType).show();

	},
	setConditionType : function(condition) {
		if (condition == "between") {
			this.conditionType = condition;
		} else {
			this.conditionType = "custom";
		}

	},
	setInput : function(condition) {
		if (condition == "between") {
			$("#betweenBox").show();
			$("#customBox").hide();
		} else {
			$("#betweenBox").hide();
			$("#customBox").show();
		}

	},
	addEvent : function() {
		var t = this;

		t.$txtDate.click(function() {
					WdatePicker({
								dateFmt : 'yyyy-MM-dd',
								isShowClear : false

							});
				});
		t.$btwnDates.click(function() {
					WdatePicker({
								dateFmt : 'yyyy-MM-dd',
								isShowClear : false
							});
				});

		t.$txtDept.deptTree({
					height : 170
				});

		t.$selItem.change(function() {
					t.setItemType($(this).val())
					t.selCtrl();
				});

		t.$selCondition.change(function() {
					t.setConditionType($(this).val());
					t.selCtrl();
				});

		$("#btnAddAnd").click(function() {
					t.addAnd();
					return false;
				});

		$("#btnAddOr").click(function() {
					t.addOr();
					return false;
				});

		$("#btnAdd").click(function() {
					t.add();
				});

		$("#btnDel").click(function() {
					t.del();
					return false;
				});
		$("#btnSearch").click(function() {
					if (!t.isFirst()) {
						var $form = $("#srhForm");
						
						$form.find("input[name='hql']").val($.trim(t.getHql()));
						
						$form.submit();
						
						parent.$("a.ui-dialog-titlebar-close").click();
						
					} else {
						swss.ui.tip("请新增条件");
					}
					return false;
				});

		$("#btnClose").click(function(){
			parent.swss.ui.modal.close();
			return false;
		});		
				
		this.$list.find("div.tr").live({
					"mouseenter" : function() {
						$(this).addClass("here");
					},
					"mouseleave" : function() {
						if (this != t.$focusItem) {
							$(this).removeClass("here");
						}
					},
					"click" : function() {
						t.$list.find("div.tr").removeClass("here");
						$(this).addClass("here");
						t.$focusItem = this;
					}
				});

	},
	init : function(options) {
		this.$selItem = $("#selItem");
		this.$selCondition = $("#selCondition");
		this.$txtValue = $("#custom-value").find("input");
		this.$txtDate = $("#custom-date").find("input");
		this.$txtDept = $("#txtDept");
		this.$btwnValues = $("#between-value").find("input");
		this.$btwnDates = $("#between-date").find("input");

		this.$list = $("#list");

		this.create(options.items, options.conditions);

		this.addEvent();
	}
};


